<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/1/17
 * Time: 15:45
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\api\controller;

use app\api\model\mysql\MallArticle;
use app\api\model\mysql\MallMessage;
use app\common\lib\Show;
use app\common\lib\Status;
use app\api\model\mysql\MallBanner;
use think\Exception;
use think\facade\Log;

class Banner extends Base
{
    public function getListInfo()
    {
        $type = $this->request->param('type', 1, 'intval');
        try {
            $lists = (new MallBanner())->getBannerList($type);
        } catch (\Exception $e) {
            Log::error("banner-getListInfo:" . $e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            return Show::json_show(Status::mini_error, "查询失败");
        }
        return Show::json_show(Status::mini_normal, "ok", [
            "banner" => $lists
        ]);
    }

    /**
     * 系统通知
     * @return \think\response\Json
     */
    public function getNotice(){
        try{
            $message =(new MallMessage())->field('id,content')->where("status",1)->order('create_time desc')->find();
        }catch (\Exception $e){
            Log::error("banner-getListInfo:" . $e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            return Show::json_show(Status::mini_error, "查询失败");
        }
        return Show::json_show(Status::mini_normal, "ok", [
            "message" => $message
        ]);
    }

    /**
     * 首页文章
     * @return \think\response\Json
     */
    public function  getIndexArticle(){

        $where =[['status','=',1],['isindex','=',1],['id','>',1]];
        try{
            $article =(new MallArticle())->field('id,title,logo,short_desc')->where($where)->order('create_time desc')->select()->toArray();
        }catch (\Exception $e){
            Log::error("banner-getListInfo:" . $e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            return Show::json_show(Status::mini_error, "查询失败");
        }
        return Show::json_show(Status::mini_normal, "ok", [
            "article" => $article??[]
        ]);
    }

    /**
     * 文章详情接口
     * @return \think\response\Json
     */
    public function getArticleDetail(){
        $id =$this->request->param('article_id',0,'intval');
        try{
            $article =(new MallArticle())->field('id,title,logo,short_desc,content')->where([['id','=',$id]])->find();
        }catch (\Exception $e){
            Log::error("banner-getListInfo:" . $e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            return Show::json_show(Status::mini_error, "查询失败");
        }
        if(empty($article)){
            return Show::json_show(Status::mini_error, "文章不存在");
        }
        $article['content'] =htmlspecialchars_decode($article['content']);
        return Show::json_show(Status::mini_normal, "ok", [
            "article" => $article??[]
        ]);
    }
}